package com.ciwong.epaper.modules.bookstore.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

/**
 * 教辅书城轮播图adapter
 * Created by luojinlei on 2017/12/22.
 */

public class BookstoreBannerAdapter extends PagerAdapter {
	private Context context;
	//存放图片的数组
	private List<String> mList;
	//图片点击的监听
	private View.OnClickListener onClickListener;

	public BookstoreBannerAdapter(Context context, List<String> mList, View.OnClickListener onClickListener) {
		this.context = context;
		this.mList = mList;
		this.onClickListener = onClickListener;
	}

	@Override
	public int getCount() {
		//返回一个比较大的值，目的是为了实现无限轮播
		return Integer.MAX_VALUE;
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		ImageView imageView = new ImageView(context);
		Glide.with(context).load(mList.get(position % mList.size())).into(imageView);
		container.addView(imageView);
		imageView.setOnClickListener(onClickListener);
		return imageView;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}
}
